<template>
  <div style="width: 100%;">
    <q-form class="q-gutter-md">
      <q-tabs v-model="tab" align="left" narrow-indicator>
        <q-tab name="basic" :label="$t('form.execSql.tabBasic')"/>
        <q-tab name="runningConfig" :label="$t('form.execSql.tabRunningConfig')"/>
      </q-tabs>
      <q-tab-panels v-model="tab" animated>
        <q-tab-panel class="row q-col-gutter-xs" name="basic">
          <q-input class="col-12 col-md-6" outlined v-model="form.name" :label="$t('form.execSql.name')" :rules="[ val => val && val.length > 0 || 'Please type something']" hint=""/>
          <q-select class="col-12 col-md-6" outlined v-model="form.datasource" emit-value map-options option-value="id" :options="datasourceOptions" :label="$t('form.execSql.datasource')" clearable hint="">
            <template v-slot:option="scope">
              <q-item v-bind="scope.itemProps" v-on="scope.itemEvents">
                <q-item-section>
                  <q-item-label>{{ scope.opt.label }}</q-item-label>
                  <q-item-label caption v-if="!scope.opt.generic">host: {{ scope.opt.host }}</q-item-label>
                  <q-item-label caption v-if="!scope.opt.generic">port: {{ scope.opt.port }}</q-item-label>
                  <q-item-label caption v-if="scope.opt.generic">url: {{ scope.opt.url }}</q-item-label>
                  <q-item-label caption v-if="scope.opt.generic">driver: {{ scope.opt.driver }}</q-item-label>
                </q-item-section>
                <q-item-section side>
                  <q-badge :label="scope.opt.category" color="primary"/>
                </q-item-section>
              </q-item>
            </template>
          </q-select>
          <q-input class="col-12" outlined v-model="form.sql" type="textarea" rows="12" label="SQL" hint=""/>
          <div class="col-6">
            <q-table :data="form.parameters" :columns="parameterColumns" :rows-per-page-options="[0]" row-key="name" separator="cell" hide-bottom :title="$t('form.execSql.tableField')">
              <template v-slot:top-right>
                <q-btn size="sm" outline color="primary" icon="add" @click="addParameter"/>
              </template>
              <template v-slot:body="props">
                <q-tr>
                  <q-td key="operate" :props="props">
                    <q-btn size="xs" outline round color="negative" icon="remove" @click="deleteParameter(props)"></q-btn>
                  </q-td>
                  <q-td key="parameter" :props="props">
                    {{ props.row.parameter }}
                    <q-popup-edit v-model="props.row.parameter" :auto-save="true">
                      <q-select autofocus outlined v-model="props.row.parameter" :options="sourceFields"/>
                    </q-popup-edit>
                  </q-td>
                </q-tr>
              </template>
            </q-table>
          </div>
          <div class="col-6 row q-col-gutter-xs">
            <q-checkbox class="col-12 col-md-5" outlined v-model="form.executeEachInputRow" :label="$t('form.execSql.executeEachRow')" hint=""/>
            <q-checkbox class="col-12 col-md-4" outlined v-model="form.variableReplace" :label="$t('form.execSql.replaceVariable')" hint=""/>
            <q-checkbox class="col-12 col-md-3" outlined v-model="form.bindParameters" :label="$t('form.execSql.bindParameter')" :disable="!form.executeEachInputRow" hint=""/>
            <q-checkbox class="col-12 col-md-5" outlined v-model="form.executeInOneStatement" :label="$t('form.execSql.executeSingleStatement')" hint=""/>
            <q-checkbox class="col-12 col-md-5" outlined v-model="form.quoteString" :label="$t('form.execSql.quoteString')" :disable="!form.executeEachInputRow" hint=""/>
          </div>
        </q-tab-panel>
        <q-tab-panel name="runningConfig">
          <q-input outlined type="number" v-model.number="form.parallel" :label="$t('form.execSql.threads')" min="1" :disable="forbiddenParallel"/>
        </q-tab-panel>
      </q-tab-panels>
  </q-form>
  </div>
</template>

<script>
import { fetchDatasourceList } from 'src/service/base/DatasourceService'
import { structure } from 'src/service/kettle/PreviewService'

const FORBIDDEN_NEXT_STEP_PARALLEL = ['SwitchCaseMeta']
const IGNORE_REPEAT_WARNING_META = ['SortRowsMeta', 'SetValueFieldMeta']
export default {
  name: 'ExecSQLMeta',
  data () {
    return {
      tab: 'basic',
      form: {
        name: null,
        datasource: null,
        sql: null,
        executeEachInputRow: false,
        executeInOneStatement: false,
        variableReplace: false,
        bindParameters: false,
        quoteString: false,
        parameters: [],
        parallel: 1,
        distribute: true
      },
      datasourceOptions: [],
      parameterColumns: [
        {
          name: 'operate',
          label: this.$t('form.execSql.columnOperate'),
          filed: 'operate',
          align: 'right',
          headerStyle: 'width: 20px'
        },
        {
          name: 'parameter',
          label: this.$t('form.execSql.columnField'),
          field: 'parameter',
          align: 'left',
          headerStyle: 'width: 150px;'
        }
      ],
      sourceFields: [],
      forbiddenParallel: false
    }
  },
  methods: {
    validate (val) {
      return val != null
    },
    loadColumns ({
      node,
      key,
      done,
      fail
    }) {
      const vm = this
      vm.$q.loading.show()
      structure({ datasourceId: vm.form.datasource, category: 'table', name: key.replace('t_', '') }).then(res => {
        vm.$q.loading.hide()
        const children = []
        res.data.forEach(element => {
          children.push({
            id: 'c_' + element.name,
            label: element.name + '【' + element.info + '】',
            selectable: false
          })
        })
        done(children)
      }).catch(err => {
        vm.$q.loading.hide()
        vm.$q.notify({
          position: 'top',
          message: err.data.error,
          color: 'negative'
        })
      })
    },
    addParameter () {
      this.form.parameters.push({
        parameter: null
      })
    },
    deleteParameter (props) {
      this.form.parameters.splice(props.rowIndex, 1)
    },
    submitForm () {
      this.$emit('propertiesForm', {
        state: true,
        mxCellProperties: this.form
      })
    }
  },
  computed: {
    previewBtnDisable () {
      return this.form.lookupFromStepName !== null || (this.form.sql === null || (this.form.sql !== null && this.form.sql.trim() === '')) || this.form.datasource === null
    }
  },
  mounted () {
    const vm = this
    const previousSteps = vm.$store.getters['etl/getPreNodes']
    vm.sourceFields = []
    const replaceFields = []
    if (previousSteps && previousSteps.length > 0) {
      previousSteps.forEach((step, i) => {
        if (i === 0 && FORBIDDEN_NEXT_STEP_PARALLEL.indexOf(step.type) >= 0) {
          vm.forbiddenParallel = true
        }
        if (step.ext !== undefined && step.ext !== 'undefined' && IGNORE_REPEAT_WARNING_META.indexOf(step.type) < 0) {
          const ext = JSON.parse(step.ext)
          if (ext.sourceFields) {
            ext.sourceFields.forEach(field => {
              vm.sourceFields.push(field)
            })
          }
          if (ext.replaceFields) {
            ext.replaceFields.forEach(field => {
              replaceFields.push(field)
            })
          }
        }
      })
    }
    replaceFields.forEach(field => {
      vm.sourceFields.splice(vm.sourceFields.indexOf(field), 1)
    })
    if (new Set(vm.sourceFields).size !== vm.sourceFields.length) {
      vm.$q.dialog({
        dark: true,
        title: vm.$t('message.error.default'),
        message: this.$t('message.error.duplicateSourceField')
      }).onOk(() => {
        this.$emit('propertiesForm', {
          state: true,
          mxCellProperties: this.form,
          ext: {
            sourceFields: []
          }
        })
      })
    }
    const mxCellValue = vm.$store.getters['etl/getMxCellForm']
    if (mxCellValue) {
      vm.form = Object.assign(vm.form, mxCellValue)
    }
    const root = vm.$store.getters['etl/getRoot']
    fetchDatasourceList({
      projectId: root.projectId
    }).then(res => {
      res.data.forEach(ele => {
        vm.datasourceOptions.push({
          id: ele.id,
          label: ele.name,
          category: ele.category,
          generic: ele.generic,
          host: ele.host,
          port: ele.port,
          url: ele.url,
          driver: ele.driver
        })
      })
    })
  }
}
</script>
